<template>
  <div>
    <!-- 搜索页面头部 -->
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="$router.back()"
      />
      <!-- 搜索组件 -->
      <van-search
        placeholder="请输入搜索关键词"
        background="pink"
        shape="round"
        v-focus
        v-model="kw"
        @input="inputFn"
        @search="searchFn"
      />
    </div>
    <!-- 搜索建议列表 -->
    <div class="sugg-list" v-if="kw.length!==0">
      <div
        class="sugg-item"
        v-for="(str, index) in suggesArr"
        :key="index"
        v-html="suggesHeigth(str)"
        @click="clickSuggesFn(str)"
      ></div>
    </div>
    <!-- 搜索历史 -->
    <div class="search-history" v-else>
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="delHistiryFn"/>
        </template>
      </van-cell>

      <!-- 历史列表 -->
      <div class="history-list">
        <span class="history-item" v-for="(str,index) in historyArr" :key="index" @click="$router.push(`/search/${str}`)">{{str}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { suggestionAPI } from '@/api'
export default {
  data () {
    return {
      kw: '', // 搜索关键字
      timer: null, // 定时器
      suggesArr: [], // 联想词汇数组
      historyArr: JSON.parse(localStorage.getItem('history')) || []
    }
  },
  methods: {
    // 删除历史记录
    delHistiryFn () {
      this.historyArr = []
      localStorage.removeItem('history')
    },
    searchFn (value) {
      // 搜索历史
      this.historyArr.push(value)
      this.historyArr = [...new Set(this.historyArr)]
      setTimeout(() => { this.$router.push(`/search/${value}`) }, 0)
    },
    clickSuggesFn (str) {
      this.historyArr.push(str)
      this.historyArr = [...new Set(this.historyArr)]
      setTimeout(() => { this.$router.push(`/search/${str}`) }, 0)
    },
    inputFn (value) {
      // 防抖,节省访问次数
      clearTimeout(this.timer)
      this.timer = setTimeout(async () => {
        if (value.length === 0) return
        const res = await suggestionAPI(value)
        console.log(res)
        // if (res.data.data.options.length === 1) console.log(111)
        this.suggesArr = res.data.data.options
      }, 500)
    },
    suggesHeigth (seggesStr) {
      if (this.suggesArr.length === 1) return
      // 因为需要全局匹配,所有写构造函数
      const reg = new RegExp(this.kw, 'gi')
      return seggesStr.replace(reg, (substring) => {
        return `<span style="color:red">${substring}</span>`
      })
    }
  },
  watch: {
    historyArr () {
      // const newArr = []
      // this.historyArr.forEach(str => {
      //   if (newArr.indexOf(str) === -1) {
      //     newArr.push(str)
      //   }
      // })
      // console.log(newArr)
      localStorage.setItem('history', JSON.stringify(this.historyArr))
    }
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: pink;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
/*搜索建议列表样式 */
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
/**搜索历史 */
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
